package com.radar.io.radar346.DataType;

import com.radar.algorithm.MathCommon;
import com.radar.common.ProductType;
import com.radar.exception.RadarDecodeError;
import com.radar.io.RadarGridData;
import com.radar.io.radar346.Product382改;
import com.radar.visualize.RasterMap;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class CAPPIParse {
    Product382改 _this;

    public CAPPIParse(Product382改 product382改) {
        _this = product382改;
    }

    public void parse(byte[] bytes, int index, Product382改.RealFile header) throws Exception {
//        int Layers = header.realObserSec.iLiTiSaoMiaoCengShu;
        int Layers = 3;
        int[] Records = new int[Layers];
        double[] MaxV = new double[Layers];
        int[] bins = new int[Layers];
        int Bins = 0;
        for (int i = 0; i < Layers; i++) {
            Records[i] = header.realObserSec.LayerParam[i].iCaiYangJinXiangShu;
            bins[i] = header.realObserSec.LayerParam[i].usRefBinNumber;
        }
        _this.Bins = Arrays.stream(bins).max().getAsInt();

        _this.azi = new double[Records[0]];  //长度为每层径向数
        _this.data3d = new double[3][Records[0]][_this.Bins];  // 切割高度  每层 径向数  最大观测距离
        MathCommon.fill(_this.data3d);
        byte[] bytdata;
        for (int cappi = 0; cappi < 3; cappi++) {
            for (int i = 0; i < Records[0]; i++) {  //循环径向数
                Product382改 product382改 = new Product382改();
                Product382改.RadialHeader radialHeader = product382改.new RadialHeader();
                bytdata = new byte[radialHeader.len];
                System.arraycopy(bytes, index, bytdata, 0, bytdata.length);
                radialHeader.parse(bytdata);
                index += bytdata.length;
                if (cappi == 0) {
                    _this.azi[i] = radialHeader.azimuth;
                }
                for (int k = 0; k < bins[cappi]; k++) {  //循环3 最大的可测距离
                    double d = (0xff & bytes[index]);
                    index += 1;
                    _this.data3d[cappi][i][k] = d;
//                    _this.data3d[cappi][i][k] = (double) ((d - 66)) / 2.0;
//                    _this.data3d[cappi][i][k] = _this.data3d[cappi][i][k] < -10 ? GisConstants.UNDEF : _this.data3d[cappi][i][k];
                }
            }
        }
    }

    public static void main(String[] args) throws RadarDecodeError, IOException {
        for (int i = 0; i < 3; i++) {
            Product382改 product346 = new Product382改();
            File f = new File("/Users/xx/DeskTop/20240129/CAPPI/20240129_202312.00.008.005.00500.S.bz2");
            product346.parseProduct(f.getAbsolutePath());
            RadarGridData project = product346.project3D(ProductType.REF, i);
            BufferedImage bufferedImage = new RasterMap(null).drawSingleGradientRaster(project.data, ProductType.REF, new Color(0, 0, 0, 0));
            ImageIO.write(bufferedImage, "png", new File(f.getAbsolutePath() + "_" + i + ".png"));
        }
    }
}
